package com.java1234.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java1234.dao.MessageDao;
import com.java1234.model.Message;
import com.java1234.model.PageBean;
import com.java1234.util.DbUtil;
import com.java1234.util.JsonUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class EndMessageListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	DbUtil dbUtil=new DbUtil();
	MessageDao messageDao=new MessageDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String page=request.getParameter("page");
		String rows=request.getParameter("rows");
		//查询功能	
				String messageId=request.getParameter("messageId");
				String Name=request.getParameter("Name");
				String head=request.getParameter("head");
				String workspace=request.getParameter("workspace");
				String members=request.getParameter("members");
				String aid=request.getParameter("aid");
				
				if(messageId==""){
					messageId="0";
				}
				if(Name==null){
					Name="";
				}
				if(head==null){
					head="";
				}
				if(workspace==null){
					workspace="";
				}
				if(members==null){
					members="";
				}
				if(aid==null){
					aid="";
				}
				Message message=new Message();
				int id = 0;
				if(messageId != null){
					id=Integer.parseInt(messageId);
				}		
				
				message.setId(id);
				message.setName(Name);
				message.setHead(head);
				message.setWorkspace(workspace);
				message.setMembers(members);
				message.setAid(aid);
				message.setSubmit("已提交");
				message.setIs_end("已结项");
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Connection con=null;
		try{
			con=dbUtil.getCon();
			JSONObject result=new JSONObject();
			JSONArray jsonArray=JsonUtil.formatRsToJsonArray(messageDao.endMessageList(con, pageBean, message));
			//int total=messageDao.messageConfirmCount(con,message);
			int total = 0;
			if(StringUtil.isNotEmpty(message.getName())||StringUtil.isNotEmpty(message.getHead())||StringUtil.isNotEmpty(message.getWorkspace())||StringUtil.isNotEmpty(message.getMembers())||StringUtil.isNotEmpty(message.getAid())){
				total=messageDao.messageSearchCount(con,message);
			}else{
				total=messageDao.messageEndCount(con,message);
			}
			result.put("rows", jsonArray);			
			result.put("total", total);
			ResponseUtil.write(response, result);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
